import { getUserInfo } from '@/api/user.js'

const state = {
	userInfo: uni.getStorageSync('userInfo') || {},
	token: uni.getStorageSync('token') || '',
	isLogin: !!uni.getStorageSync('token'),
	wxRegisterData: {}  // 微信授权注册需要的信息
}

const mutations = {
	SET_TOKEN(state, data){
		uni.setStorageSync('token', data)
		state.token = data
	},
	SET_USERINFO(state, data){
		uni.setStorageSync('userInfo', data)
		state.userInfo = data
	},
	SET_ISLOGIN(state, data){
		state.isLogin = data
		
	},
	// 微信授权注册需要的信息
	SET_WXREGISTERDATA(state, data){
		state.wxRegisterData = data
	}
}

const actions = {
	getUserInfo({commit}, data){
		return new Promise((resolve,reject) => {
			getUserInfo().then(res => {
				commit('SET_USERINFO', res.result)
				resolve(res)
			})
		})
	},
	logout({commit}){
		commit('SET_TOKEN', '')
		uni.removeStorageSync('token')
		commit('SET_USERINFO', {})
		uni.removeStorageSync('userInfo')
		commit('SET_ISLOGIN', false)
	}
}

export default{
	namespaced: true,
	state,
	mutations,
	actions
}